
package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.AdminSysConfigAddDto;
import com.dog.basics.bean.dto.admin.common.AdminSysConfigEditDto;
import com.dog.basics.bean.dto.admin.common.AdminSysConfigFromDto;
import com.dog.basics.bean.model.common.SysConfig;
import com.dog.basics.bean.vo.admin.common.AdminSysConfigVo;
import com.dog.basics.bean.vo.api.common.ApiSysConfigVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.SysConfigMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.SysConfigService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数配置
 *
 * @author cy code generator
 * @date 2021-03-18 09:56:14
 */
@Service
@AllArgsConstructor
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    /**
     * @Description: 分页
     * @Param: status
     * @return: IPage
     * @Author: cy
     * @Date:
     */
    @Override
    public IPage<AdminSysConfigVo> getAdminPageList(PageParam page, AdminSysConfigFromDto obj) {
        IPage iPage = this.page(page, Wrappers.<SysConfig>lambdaQuery()
                .like(StrUtil.isNotBlank(obj.getCode()), SysConfig::getCode, obj.getCode())
                .like(StrUtil.isNotBlank(obj.getName()), SysConfig::getName, obj.getName())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), SysConfig::getCreateTime));
        List<SysConfig> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            AdminSysConfigVo newsVo = new AdminSysConfigVo();
            BeanUtil.copyProperties(e, newsVo);
            return newsVo;
        }).collect(Collectors.toList()));
        return iPage;
    }

    /**
     * @Description: 查询详情
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    public AdminSysConfigVo getAdminObjXq(Long id) {
        SysConfig dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        AdminSysConfigVo newsVo = new AdminSysConfigVo();
        BeanUtil.copyProperties(dogNews, newsVo);
        return newsVo;
    }

    /**
     * api获取配置信息
     *
     * @param code
     * @return SysConfigPageVo
     * @author fengjukang
     */
    @Override
    public ApiSysConfigVo getApiSysConfig(String code) {
        List<SysConfig> list = this.list(Wrappers.<SysConfig>lambdaQuery().eq(SysConfig::getCode, code).orderByDesc(SysConfig::getCreateTime));
        Validator.validateTrue(CollUtil.isNotEmpty(list), "common.noData");
        ApiSysConfigVo aboutUsVo =BeanUtil.toBean(list.get(0), ApiSysConfigVo.class);
        return aboutUsVo;
    }

    /**
     * @Description: 增加
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdminObj(AdminSysConfigAddDto obj) {
        SysConfig dogNews = BeanUtil.copyProperties(obj, SysConfig.class);
        dogNews.setCreateTime(DateUtil.currentSeconds());
        dogNews.setCreateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.save(dogNews), "common.save");
        return Boolean.TRUE;
    }


    /**
     * @Description: 修改
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editAdminObj(AdminSysConfigEditDto obj) {
        SysConfig dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "common.noData");
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.updateById(dogNews), "common.edit");
        return Boolean.TRUE;
    }


    /**
     * @Description: 删除
     * @Param: ids
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        SysConfig dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        Validator.validateTrue(this.removeById(id), "common.del");
        return Boolean.TRUE;
    }

}
